package com.xiaoxiaoniu.airoleplay.controller;

import com.xiaoxiaoniu.airoleplay.services.AIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * API测试控制器
 * 提供用于测试AI服务连接和基本功能的端点
 */
@RestController
@RequestMapping("/api/test")
public class APITestController {

    @Autowired
    private AIService aiService;

    /**
     * 测试AI服务连接状态
     * 
     * @return 连接测试结果
     */
    @GetMapping("/ai-connection")
    public CompletableFuture<Map<String, Object>> testAIConnection() {
        return aiService.testAPIConnection().thenApply(success -> {
            Map<String, Object> result = new HashMap<>();
            result.put("success", success);
            result.put("message", success ? "AI API连接正常" : "AI API连接失败");
            result.put("timestamp", System.currentTimeMillis());
            return result;
        });
    }

    /**
     * 简单聊天测试
     * 
     * @return 聊天测试结果
     */
    @GetMapping("/simple-chat")
    public CompletableFuture<Map<String, Object>> simpleChatTest() {
        return aiService.getAIResponse("测试角色", "你好，请简单回复测试成功").thenApply(reply -> {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("reply", reply);
            result.put("timestamp", System.currentTimeMillis());
            return result;
        }).exceptionally(e -> {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("error", e.getMessage());
            result.put("timestamp", System.currentTimeMillis());
            return result;
        });
    }
}